package com.das.mechanic_base.widget;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.das.mechanic_base.R;
import com.das.mechanic_base.app.X3Constent;
import com.das.mechanic_base.bean.main.ValidCarBean;
import com.das.mechanic_base.utils.SpHelper;
import com.das.mechanic_base.utils.X3DateUtils;
import com.das.mechanic_base.utils.X3GlideRoundTransform;
import com.das.mechanic_base.utils.X3StringUtils;
import com.umeng.analytics.MobclickAgent;

public class X3PickUpListDialog extends Dialog implements View.OnClickListener {

    private ValidCarBean numBean;
    private Context context;
    private long receiveBaseId;
    private long carId;
    private long carOwnid;
    private LinearLayout ll_bottom;
    private int status;
    private RelativeLayout rl_content;
    private Button btn_creat;
    private TextView tv_notice;
    private String km;
    private String money_code;
    private TextView tv_receive_id;
    private RelativeLayout rl_car;
    private ImageView iv_car_right;
    private RelativeLayout rl_header;
    private TextView tv_order_time;
    private View v_not_pay;
    private TextView tv_check;
    private TextView tv_no_pay;
    private Drawable noPayDrawable;
    private TextView tv_name;
    private RequestOptions myOptions;
    private ImageView iv_header;
    private ImageView iv_car;
    private TextView tv_brand_name;
    private TextView tv_car_style;
    private TextView tv_car_time;
    private TextView tv_miles;
    private TextView tv_cancel;
    private String carNumember;
    private boolean isSdk;


    public X3PickUpListDialog(@NonNull Context context) {
        super(context, R.style.pointDialog);
        this.context = context;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.x3_pick_up_list_dialog);
        setCancelable(true);
        initView();
    }

    private void initView() {
        isSdk = (boolean) SpHelper.getData(X3Constent.IS_OPEN_SDK, false);
        noPayDrawable = ContextCompat.getDrawable(context,R.mipmap.x3_car_num_not_paid);
        noPayDrawable.setBounds(0, 0, noPayDrawable.getMinimumWidth(), noPayDrawable.getMinimumHeight());
        myOptions = new RequestOptions()
                 .centerCrop()
                 .dontAnimate()
                 .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                 .transform(new X3GlideRoundTransform(context, 6));
        ll_bottom = findViewById(R.id.ll_bottom);
        rl_content = findViewById(R.id.rl_content);
        tv_notice = findViewById(R.id.tv_notice);
        btn_creat = findViewById(R.id.btn_creat);
        btn_creat.setOnClickListener(this);
        tv_receive_id = findViewById(R.id.tv_receive_id);
        rl_car = findViewById(R.id.rl_car);
        rl_car.setOnClickListener(this);
        iv_car_right = findViewById(R.id.iv_car_right);
        rl_header = findViewById(R.id.rl_header);
        rl_header.setOnClickListener(this);
        tv_order_time = findViewById(R.id.tv_order_time);
        v_not_pay = findViewById(R.id.v_not_pay);
        tv_check = findViewById(R.id.tv_check);
        tv_no_pay = findViewById(R.id.tv_no_pay);
        tv_name = findViewById(R.id.tv_name);
        iv_header = findViewById(R.id.iv_header);
        iv_car = findViewById(R.id.iv_car);
        tv_brand_name = findViewById(R.id.tv_brand_name);
        tv_car_style = findViewById(R.id.tv_car_style);
        tv_car_time = findViewById(R.id.tv_car_time);
        tv_miles = findViewById(R.id.tv_miles);
        tv_cancel = findViewById(R.id.tv_cancel);
        tv_cancel.setOnClickListener(this);
        tv_check.setOnClickListener(this);
    }

    public void changeData(ValidCarBean numBean,String carNumember){
        this.carNumember = carNumember;
        this.numBean = numBean;

        receiveBaseId = 0;
        if (numBean == null) {
            return;
        }
        this.carId = numBean.getCarId();
        this.carOwnid = numBean.getCarOwnerUserId();

        boolean isShowNotPay = false;
        ll_bottom.setVisibility(View.VISIBLE);
        rl_content.setVisibility(View.VISIBLE);

        // 该车辆是否首次到店
        boolean exist = numBean.isExist();
        if (!exist) {
            receiveBaseId = numBean.getReceiveBaseId();
            status = (receiveBaseId == 0) ? 0 : 1;
            if (receiveBaseId == 0) {
                rl_content.setVisibility(View.GONE);
                tv_notice.setText("" + context.getString(R.string.x3_car_first_go));
                btn_creat.setText("" + context.getString(R.string.x3_affirm_create_work));
            } else {
                tv_notice.setText("" + context.getString(R.string.x3_car_first_go_and_no_pay));
                isShowNotPay = true;
            }
        } else {
            //系统有车了
            receiveBaseId = numBean.getReceiveBaseId();
            status = (receiveBaseId != 0) ? 2 : 0;
            if (receiveBaseId != 0) {
                tv_notice.setText("" + context.getString(R.string.x3_car_first_go_and_no_pay));
                btn_creat.setText("" + context.getString(R.string.x3_look_work));
                isShowNotPay = true;

            } else {
                int times = (int) numBean.getTimes();

                if (times == 0) {
                    /**
                     * 首次到店
                     */
                    tv_notice.setText("" + context.getString(R.string.x3_car_first_go));
                } else {
                    String timeStr = "";
                    switch (times) {
                        case 1:
                            timeStr = context.getString(R.string.x3_crm_car_visit_once_small);
                            break;
                        case 2:
                            timeStr = context.getString(R.string.x3_crm_car_visit_twice_small);
                            break;
                        default:
                            timeStr = String.format(context.getString(R.string.x3_crm_car_visit_times_small),times);
                            break;
                    }
                    tv_notice.setText(context.getString(R.string.x3_car_go_and_check) + " " + timeStr);
                }
                btn_creat.setText("" + context.getString(R.string.x3_affirm_create_work));
            }
        }

        setCarBean(numBean,isShowNotPay);
    }

    private void setCarBean(ValidCarBean numBean,boolean isShowNotPay) {
        km = (String) SpHelper.getData(X3Constent.MILEAGEUNIT, "km");
        money_code = (String) SpHelper.getData(X3Constent.MONEYCODE, "£");

        // 车牌号
        String carNum = numBean.getCarNum();

        tv_receive_id.setVisibility(isShowNotPay ? View.VISIBLE : View.GONE);
//        rl_car.setClickable(!isShowNotPay);
//        iv_car_right.setVisibility(isShowNotPay ? View.GONE : View.VISIBLE);
        iv_car_right.setVisibility(View.VISIBLE);
        rl_header.setVisibility(isShowNotPay ? View.GONE : View.VISIBLE);
        tv_order_time.setVisibility(isShowNotPay ? View.VISIBLE : View.GONE);
        v_not_pay.setVisibility(isShowNotPay ? View.VISIBLE : View.GONE);
        tv_check.setVisibility(isShowNotPay ? View.VISIBLE : View.GONE);
        ll_bottom.setVisibility(isShowNotPay ? View.GONE : View.VISIBLE);

        if (isShowNotPay) {
            /**
             * 展示未支付布局
             */
            tv_no_pay.setText(context.getString(R.string.x3_no_paid));
            tv_no_pay.setCompoundDrawables(noPayDrawable,null,null,null);

            // TODO 接车单号
            String key = X3StringUtils.isEmpty(numBean.getReceiveBaseKey()) ? "-" : numBean.getReceiveBaseKey();
            tv_receive_id.setText(String.format(context.getString(R.string.x3_service_order_id),key));
            // TODO 下单时间
            tv_order_time.setText(String.format(context.getString(R.string.x3_create_order_time), X3DateUtils.MonthWithDay(numBean.getReceiveBaseUnPayTime())));

        }else {
            tv_no_pay.setText(X3StringUtils.isEmpty(carNum) ? carNumember : carNum);
            tv_no_pay.setCompoundDrawables(null,null,null,null);
        }

        // 用户姓名
        String carOwnerName = X3StringUtils.isEmpty(numBean.getCarOwnerName()) ? "-" : numBean.getCarOwnerName();
        tv_name.setText(carOwnerName);

        Glide.with(context).load(X3StringUtils.getImageUrl(numBean.getImgUrl())).apply(new RequestOptions().
                apply(myOptions).error(R.mipmap.x3_car_owner_header).
                placeholder(R.mipmap.x3_car_owner_header)).into(iv_header);

        // 车辆头像
        Glide.with(context).load(X3StringUtils.getImageUrl(numBean.getCarImgUrl())).apply(new RequestOptions().
                apply(myOptions).error(R.mipmap.x3_car_brand_icon).
                placeholder(R.mipmap.x3_car_brand_icon)).into(iv_car);

        String brandName = X3StringUtils.isEmpty(numBean.getCarBrandName()) ? "-" : numBean.getCarBrandName();
        tv_brand_name.setText(brandName);

        String styleName = X3StringUtils.isEmpty(numBean.getCarStyleName()) ? "-" : numBean.getCarStyleName();
        tv_car_style.setText(styleName);

        // 出厂时间
        if (!X3StringUtils.isEmpty(numBean.getProductionDate())) {
            String productionDate = numBean.getProductionDate();
            tv_car_time.setText(X3DateUtils.MonthWithYear(productionDate) + "");
        } else {
            tv_car_time.setText("-");
        }
        // 日均行驶里程
        double perMiles = numBean.getPerMiles();
        tv_miles.setText(X3StringUtils.formatTwoDouble(perMiles) + km);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_cancel) {
            dismiss();
        } else if (id == R.id.tv_check) {
            if (ionClickCreate != null) {
                ionClickCreate.ionclickcheck(receiveBaseId);
            }
//                dismiss();
        } else if (id == R.id.btn_creat) {
            if (ionClickCreate != null) {
                ionClickCreate.ionClickCreate(status, carNumember, receiveBaseId);
            }
            dismiss();
        } else if (id == R.id.rl_header) {
            if (isSdk) {
                return;
            }
            if (ionClickCreate != null) {
                ionClickCreate.iOnClickHeader(carOwnid);
            }
        } else if (id == R.id.rl_car) {
            if (isSdk) {
                return;
            }
            if (ionClickCreate != null) {
                ionClickCreate.iOnClickCar(carId);
            }
        }
    }
    @Override
    protected void onStart() {
        super.onStart();
        MobclickAgent.onPageStart("车牌号搜索完车辆信息弹窗");
    }

    @Override
    protected void onStop() {
        super.onStop();
        MobclickAgent.onPageEnd("车牌号搜索完车辆信息弹窗");
    }

    public interface IonClickCreate {
        void ionclickcheck(long receiveBaseId);
        void ionClickCreate(int status, String carNum, long receivedId);
        void iOnClickHeader(long carOwnid);
        void iOnClickCar(long carId);
    }
    public IonClickCreate ionClickCreate;
    public void setIonClickCreate(IonClickCreate ionClickCreate){
        this.ionClickCreate = ionClickCreate;
    }

}
